﻿using System;
using System.Windows.Forms;
using System.IO;
using System.Collections.Generic;

namespace ProjectPacker
{
    public partial class MainForm : Form
    {
        MyConfig mc = new MyConfig();
        UpdateInfo ui = new UpdateInfo();
        ConfigForm cf;
        ProgressForm pf;
        List<String> items;

        public MainForm()
        {
            InitializeComponent();
        }
        private void button_Config_Click(object sender, EventArgs e)
        {
            cf = new ConfigForm(ref mc);
            cf.ShowDialog();
            FormRefresh();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            Init();
        }
        public void Init()
        {
            if (!Directory.Exists(MyConfig.DataFolder))
            {
                Directory.CreateDirectory(MyConfig.DataFolder);
            }
            if (!Directory.Exists(MyConfig.TempFolder))
            {
                Directory.CreateDirectory(MyConfig.TempFolder);
            }

            if (ConfigWorker.FileDetect() == ConfigWorker.States.Exception)
            {
                ConfigWorker.CreatNew(ref mc);
            }
            if (ConfigWorker.Load(ref mc) == ConfigWorker.States.OK)
            {
                FormRefresh();
            }
            else
            {
                ConfigWorker.CreatNew(ref mc);
            }
            try
            {
                UpdateInfo.Load(ref ui, MyConfig.DataFolder + "\\" + mc.SelectedUpdateInfo);
                ui.SetDefault();
                textBox_Date.Text = ui.NewVersion.ToString();
                textBox_FileName.Text = ui.FileName;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
        public void FormRefresh()
        {
            comboBox_Config.Items.Clear();
            DirectoryInfo dir = new DirectoryInfo(MyConfig.DataFolder);
            FileInfo[] files = dir.GetFiles("*.Strings");
            foreach (FileInfo file in files)
            {
                comboBox_Config.Items.Add(file.Name);
            }
            if (mc.SelectConfig != null && comboBox_Config.Items.Contains(mc.SelectConfig))
            {
                comboBox_Config.SelectedItem = mc.SelectConfig;
            }
            comboBox_UpdateInfo.Items.Clear();
            dir = new DirectoryInfo(MyConfig.DataFolder);
            files = dir.GetFiles("*.txt");
            foreach (FileInfo file in files)
            {
                comboBox_UpdateInfo.Items.Add(file.Name);
            }
            if (mc.SelectedUpdateInfo != null && comboBox_UpdateInfo.Items.Contains(mc.SelectedUpdateInfo))
            {
                comboBox_UpdateInfo.SelectedItem = mc.SelectedUpdateInfo;
            }
        }
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                mc.SelectedUpdateInfo = comboBox_UpdateInfo.SelectedItem.ToString();

                ConfigWorker.Save(mc);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void button_Pack_Click(object sender, EventArgs e)
        {

            try
            {
                Directory.Delete(MyConfig.TempFolder, true);
                Directory.CreateDirectory(MyConfig.TempFolder);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            items = DataManager.StringListLoad(MyConfig.DataFolder + "\\" + comboBox_Config.SelectedItem.ToString());
            backgroundWorker1.RunWorkerAsync();
            MessageSender.Progress_Total = items.Count + 4;
            MessageSender.Progress_Done = 0;
            pf = new ProgressForm();
            pf.Show();
        }

        private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            try
            {
                PathWorker.PackPathCreate(items);
                Packer7z.Pack(ui.NewVersion + ".7z");
                MessageSender.Progress_Done++;
                if (File.Exists(ui.FileName))
                {
                    File.Delete(ui.FileName);
                }
                MessageSender.Progress_Done++;
                File.Move(ui.NewVersion + ".7z", ui.FileName);
                MessageSender.Progress_Done++;
                ui.FileLength = (new FileInfo(ui.FileName)).Length;
                UpdateInfo.Save(ui);
                MessageSender.Progress_Done++;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
                pf.Dispose();
            }

        }

        private void comboBox_Config_SelectedIndexChanged(object sender, EventArgs e)
        {
            mc.SelectConfig = (String)comboBox_Config.SelectedItem;
        }

        private void button_SetDate_Click(object sender, EventArgs e)
        {
            ui.SetDate(textBox_Date.Text);
            textBox_FileName.Text = ui.FileName;
        }

        private void comboBox_UpdateInfo_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                UpdateInfo.Load(ref ui, MyConfig.DataFolder + "\\" + comboBox_UpdateInfo.SelectedItem.ToString());
                ui.SetDefault();
                textBox_Date.Text = ui.NewVersion.ToString();
                textBox_FileName.Text = ui.FileName;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
